    //
//  KDSetUP.m
//  iStock Global
//
//  Created by Andy on 2011/1/7.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "KDSetUP.h"
#import "MV_TableViewCell.h"
@implementation KDSetUP
@synthesize delegate;
@synthesize delegate_UIPopoverController;

#pragma mark -
#pragma mark Initialization

- (void)loadView {
	
	UIView *mainview;
	mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
	self.view=mainview;
    [mainview release];
    
	self.view.backgroundColor=[UIColor lightTextColor];	
	UINavigationBar *nav;
	
	nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];	
	
	UIBarButtonItem *barButton ;
	barButton = [[UIBarButtonItem alloc] initWithTitle:  NSLocalizedString(@"Done",@"完成")  style:UIBarButtonItemStylePlain target:self action:@selector(slideOut)];
	UINavigationItem *item;
	item = [[UINavigationItem alloc] initWithTitle:NSLocalizedString(@"KD",@"KD 設定")  ];
	[item setRightBarButtonItem: barButton];
	[nav pushNavigationItem:item animated:NO];	
	[item release];
    
	mvarray=[[NSMutableArray alloc]init];
    [mvarray addObject:@"KD"];
	[mvarray addObject:@"N"];
	[mvarray addObject:@"M1"];
	[mvarray addObject:@"M2"];
	 
	
	myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 320.0f, self.view.frame.size.height)  style:UITableViewStyleGrouped];
    myTable.dataSource = self;	//連結資料 UITableViewDataSource
	myTable.delegate=self; //連結動作 UITableViewDelegate	
	[self.view addSubview:myTable];
	
	
	
	
	
	//[self.view addSubview:headerbutton];
	[mainview addSubview:nav ];	
	
	pick=[[UIPickerView alloc ]initWithFrame:CGRectMake(0.0f, self.view.frame.size.height-216, 320.0f, 216.0f)];
	pick.delegate=self;
	pick.showsSelectionIndicator=YES;
	[pick reloadAllComponents];	
	
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	
	if ([prefs objectForKey:@"number_KD_N"]==nil) {
		[prefs setObject:[NSString stringWithFormat:@"%i",5] forKey:@"number_KD_N"];
		[prefs synchronize];	
		[pick selectRow:9 inComponent:0 animated:YES];		
	} 
	else {
		[pick selectRow:[[prefs objectForKey:@"number_KD_N"] intValue] inComponent:0 animated:YES];		
		
	}
	
	if ([prefs objectForKey:@"number_KD_M1"]==nil) {
		[prefs setObject:[NSString stringWithFormat:@"%i",10] forKey:@"number_KD_M1"];
		[prefs synchronize];	
		[pick selectRow:3 inComponent:1 animated:YES];		
	} 
	else {
		[pick selectRow:[[prefs objectForKey:@"number_KD_M1"] intValue] inComponent:1 animated:YES];		
		
	}
	
	if ([prefs objectForKey:@"number_KD_M2"]==nil) {
		[prefs setObject:[NSString stringWithFormat:@"%i",20] forKey:@"number_KD_M2"];
		[prefs synchronize];	
		[pick selectRow:3 inComponent:2 animated:YES];		
	} 
	else {
		[pick selectRow:[[prefs objectForKey:@"number_KD_M2"] intValue] inComponent:2 animated:YES];		
		
	}	
	 	
	
	
	
	
	//pick.hidden=YES;
	[self.view addSubview:pick];	
	
	
	
	
}

#pragma mark -
#pragma mark  退出至上一層
-(void)slideOut{
		
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        [delegate_UIPopoverController dismissPopoverAnimated:YES]; 
    }    
	else{
       [self.delegate KDSetUP_DidFinish:self];
    }
    
    
    
}

#pragma mark -
#pragma mark  表格事件

//區塊數量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}
//區塊與列數量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [mvarray count];
}
//表格內容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *CellIdentifier = @"MV_TableViewCell";
    
    MV_TableViewCell *cell =(MV_TableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];	
	cell = [[[MV_TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	cell.code.text=[mvarray objectAtIndex:indexPath.row];
	if (indexPath.row==0 ) {
	 	//開關
		UISwitch *onoff;
		onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
		onoff.tag=100;  
		[onoff addTarget: self action: @selector(switchChange:) forControlEvents: UIControlEventValueChanged];
		
		
		onoff.on=[[prefs objectForKey: @"isKDindicator" ] boolValue]; 
		
		[self.view addSubview: onoff];
		cell.accessoryView=onoff;
		[onoff release];	
	}	
	
	
	if (indexPath.row==1 ) {
		cell.name.text=[NSString stringWithFormat:@"%i",[[prefs objectForKey: @"number_KD_N"  ] intValue]];
	}
	if (indexPath.row==2 ) {
		cell.name.text=[NSString stringWithFormat:@"%i",[[prefs objectForKey: @"number_KD_M1"  ] intValue]];
	}
	if (indexPath.row==3 ) {
		cell.name.text=[NSString stringWithFormat:@"%i",[[prefs objectForKey: @"number_KD_M2"  ] intValue]];
	}	
	
	
	
	//不讓開關點選時出現藍色
	cell.selectionStyle=UITableViewCellSelectionStyleNone;
	
	
	
	return cell;	
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
	
	
	
}
#pragma mark -
#pragma mark  按鈕轉換
-(void)switchChange:(id) sender{
	
	UISwitch *onoff = (UISwitch *) sender;	
	
	
	switch (onoff.tag) {
		case 100:{
			//搜尋是否要使用名稱
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSNumber numberWithBool: onoff.on]  forKey:@"isKDindicator"];
			[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
			
			break;
		}
		 
			
		default:
			break;
	}
	
	
	[myTable reloadData];
	
	
}




#pragma mark -
#pragma mark  策略選擇器

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	return 3;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	
	return 365;
	
	
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	
	return [NSString stringWithFormat:@"%i",row];
	
	
	
	
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
	
	if (row==0) {
		[pick selectRow:9 inComponent:component animated:YES];
	}
	else {
		if (component==0) {
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSString stringWithFormat:@"%i",row] forKey:@"number_KD_N" ];
			[prefs synchronize];			
			
		}
		if (component==1) {
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSString stringWithFormat:@"%i",row] forKey:@"number_KD_M1" ];
			[prefs synchronize];			
			
		}		
		if (component==2) {
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs setObject:[NSString stringWithFormat:@"%i",row] forKey:@"number_KD_M2" ];
			[prefs synchronize];			
			
		}					    
		[myTable reloadData];		
		
	}
	
}






- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
